//
//  CQWeiBo.m
//  Delicious
//
//  Created by yilong zhang on 11-11-25.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "CQWeiBo.h"

@implementation CQWeiBo

@synthesize m_QWeiboDelegate;
@synthesize m_strUserName;
@synthesize m_strTokenKey;
@synthesize m_strTokenSecret;

static CQWeiBo  *g_Instance;          //全局

+(id)allocWithZone:(NSZone *)zone
{
    @synchronized(self)
    {
        if (nil == g_Instance)
        {
            g_Instance = [super allocWithZone:zone];
        }
    }
    
    return g_Instance;
}

+(CQWeiBo*)DefaultQQwb
{
    if (nil == g_Instance)
    {
        g_Instance = [[CQWeiBo alloc] init];
    }
    
    return g_Instance;
}

+(void)DestroyQQwb
{
    [g_Instance release];
    g_Instance = nil;
}

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    
    return self;
}

-(void)dealloc
{
    [m_strTokenSecret release];
    [m_strTokenKey release];
    [m_strUserName release];
    
    [super dealloc];
}

- (void)loadDefaultKey
{
	self.m_strTokenKey = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"qq_key_%@", m_strUserName]];
	self.m_strTokenSecret = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"qq_secret_%@", m_strUserName]];
    //NSLog(@"%@__%@", self.m_strTokenKey, self.m_strTokenSecret);
}

- (void)saveDefaultKey
{
    [[NSUserDefaults standardUserDefaults] setValue:self.m_strTokenKey forKey:[NSString stringWithFormat:@"qq_key_%@", m_strUserName]];
	[[NSUserDefaults standardUserDefaults] setValue:self.m_strTokenSecret forKey:[NSString stringWithFormat:@"qq_secret_%@", m_strUserName]];
	[[NSUserDefaults standardUserDefaults] synchronize];
}

- (NSString*)parseTokenKeyWithResponse:(NSString *)aResponse IsSave:(BOOL)issave
{
	NSDictionary *params  = [NSURL parseURLQueryString:aResponse];
    NSLog(@"%@", params);
    if (issave)
    {
        self.m_strTokenKey    = [params objectForKey:@"oauth_token"];
        self.m_strTokenSecret = [params objectForKey:@"oauth_token_secret"];
        [self saveDefaultKey];
    }
    
    return [params objectForKey:@"oauth_token"];
}

-(void)LoginQQWeibo:(UIViewController*)incontroller Name:(NSString*)name
{
    self.m_strUserName = name;
    
    [self loadDefaultKey];
    
    if (([self.m_strTokenKey isEqualToString:@""]) || ([self.m_strTokenSecret isEqualToString:@""]) || (nil == self.m_strTokenKey) || (nil == self.m_strTokenSecret))
    {
        QVerifyWebViewController *qver = [[QVerifyWebViewController alloc] init];
        [incontroller presentModalViewController:qver animated:YES];
        [qver release];
    }
    else 
    {
        [self LoginQQWeiboSuc];
    }
}

-(void)LoginQQWeiboSuc
{
    [m_QWeiboDelegate DidLoginQQWeibo:self State:0];
}

-(void)LoginCancel
{
    [m_QWeiboDelegate DidLoginQQWeibo:self State:1];
}

-(void)LogoutQQWeibo
{
    //[[NSUserDefaults standardUserDefaults] removeObjectForKey:qq_AppTokenKey];
    //[[NSUserDefaults standardUserDefaults] removeObjectForKey:qq_AppTokenSecret];
    //[[NSUserDefaults standardUserDefaults] synchronize];
    m_strUserName    = nil;
    m_strTokenKey    = nil;
    m_strTokenSecret = nil;
}

-(void)ShareToQWeibo:(NSString*)content ImageFile:(NSString*)filepath
{
    QWeiboSyncApi *api = [[[QWeiboSyncApi alloc] init] autorelease];
    
    [api publishMsgWithConsumerKey:QQ_AppKey consumerSecret:QQ_AppSecret accessTokenKey:m_strTokenKey accessTokenSecret:m_strTokenSecret content:content imageFile:filepath resultType:RESULTTYPE_JSON];
}

@end
